VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsDownloadTransactions"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private mcnDBFConn As ADODB.Connection
Private mcnSQLConn As ADODB.Connection
Private mblnOverwrite As Boolean

Private mstrDBFPath As String

Private arrSales() As String
Private arrSales_T() As String
Private arrCollection() As String
Private arrCollection_T() As String

Private strDL As String, strTranNo As String

Private lSpaceOK As Boolean

Public Property Get oDBFConn() As ADODB.Connection
    Set oDBFConn = mcnDBFConn
End Property

Public Property Set oDBFConn(ByVal toNewDBFConn As ADODB.Connection)
    Set mcnDBFConn = toNewDBFConn
    mstrDBFPath = mcnDBFConn.DefaultDatabase
End Property

Public Property Get oSQLConn() As ADODB.Connection
    Set oSQLConn = mcnSQLConn
End Property

Public Property Set oSQLConn(ByVal toNewSQLConn As ADODB.Connection)
    Set mcnSQLConn = toNewSQLConn
End Property

Public Property Get lOverwrite() As Boolean
    lOverwrite = mblnOverwrite
End Property

Public Property Let lOverwrite(ByVal tblnNewOverwrite As Boolean)
    mblnOverwrite = tblnNewOverwrite
End Property

Public Function DownloadDR(tdteFrom As Date, tdteTo As Date) As Boolean
        
    cn.BeginTrans
    cn.Execute ("UPDATE DR SET lPrintPosted = 1  WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cDelCode IS NOT NULL AND RTRIM(LTRIM(cDelCode)) <> '') AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT A.cCompanyID, A.cDRNo, A.lPrintPosted AS lUpload, A.dDate, A.cType, A.cDelCode, B.cName, A.cRemarks, A.dReceived, A.dReturned, A.cInvNo, " & _
        "A.nGross, A.cStatus, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, A.cApprovedBy, A.cProjNo, A.cDocType, A.cPCCode " & _
        "FROM DR A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCompanyID = B.cCompanyID AND A.cDelCode = B.cCode " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cDelCode IS NOT NULL AND RTRIM(LTRIM(A.cDelCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.dDate ", sFilePath & "DR.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM DR_T A " & _
        "LEFT OUTER JOIN DR B ON A.cDRNo = B.cDRNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cDelCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cDelCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cDRNo ", sFilePath & "DR_T.itg "
    
    'Audit trail
    UpdateLogFile "Downloading Transaction", "DR", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
        
End Function

Public Function DownloadSales(tdteFrom As Date, tdteTo As Date) As Boolean

    cn.BeginTrans
    cn.Execute ("UPDATE SALES SET lPrintPosted = 1  WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cCode IS NOT NULL AND RTRIM(LTRIM(cCode)) <> '') AND cCompanyID = '" & COID & "'")
            
    ExtractRS "SELECT A.cCompanyID, A.cInvNo, A.lPrintPosted AS lUpload, A.dDate, A.cType, A.cCode, D.cName, A.cMSCode, A.cSMan, " & _
        "A.cTerm, A.cIntTerm, A.cPayType, A.nGross, A.nReturn, A.nPaid, A.nCredit, A.nDebit, A.nBalance, " & _
        "A.nCOG, A.nTPCGross, A.nTPCPaid, A.cTPCName, A.cStatus, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, " & _
        "A.cApprovedBy, A.lBad, A.lAllow, A.dAllowDate, A.dBadDate, A.cRemarks, A.cPDCode, A.cDocType, " & _
        "A.cPCCode, A.cProjInvType, A.cProjNo, CAST(B.cValue AS INT) AS nTerm, CAST(C.cValue AS INT) AS nIntTerm " & _
        "FROM SALES A " & _
        "LEFT OUTER JOIN PARAMETER_USER B ON A.cCompanyID = B.cCompanyID AND A.cTerm = B.cParamName " & _
        "LEFT OUTER JOIN PARAMETER_USER C ON A.cCompanyID = C.cCompanyID AND A.cIntTerm = C.cParamName " & _
        "LEFT OUTER JOIN CLIENT D ON A.cCompanyID = D.cCompanyID AND A.cCode = D.cCode " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.dDate ", sFilePath & "SALES.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM SALES_T A " & _
        "LEFT OUTER JOIN SALES B ON A.cInvNo = B.cInvNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cInvNo ", sFilePath & "SALES_T.itg "
   
    ExtractRS "SELECT * FROM SALES_PROJECT WHERE cCompanyID = '" & COID & "' ORDER BY cInvNo", sFilePath & "SALES_PROJECT.ITG "
        
    'Audit trail
    UpdateLogFile "Downloading Transaction", "Sales", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else: cn.RollbackTrans
    End If

End Function

Public Function DownloadARAdj(tdteFrom As Date, tdteTo As Date) As Boolean
    cn.BeginTrans
    cn.Execute ("UPDATE AR SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cCode IS NOT NULL AND RTRIM(LTRIM(cCode)) <> '') AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT A.cCompanyID, A.cTranNo, A.lPrintPosted AS lUpload, A.dDate, A.cType, A.cCode, B.cName, A.nGross, A.cRemarks, " & _
        "A.lSalesReturn, A.cWRRNo, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, A.cApprovedBy " & _
        "FROM AR A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' AND A.lApproved = 1 " & _
        "AND A.lApproved = 1 ORDER BY A.dDate ", sFilePath & "AR.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM AR_T A " & _
        "LEFT OUTER JOIN AR B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' AND B.lApproved = 1 " & _
        "ORDER BY A.cTranNo ", sFilePath & "AR_T.itg "
        
    'Audit trail
    UpdateLogFile "Downloading Transaction", "AR Adjustment", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
End Function

Public Function DownloadCMProd(tdteFrom As Date, tdteTo As Date) As Boolean
    
    cn.BeginTrans
    cn.Execute ("UPDATE CM SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cCode IS NOT NULL AND RTRIM(LTRIM(cCode)) <> '') AND cCompanyID = '" & COID & "'")
    cn.Execute ("UPDATE AR SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cCode IS NOT NULL AND RTRIM(LTRIM(cCode)) <> '') AND cCompanyID = '" & COID & "' AND SUBSTRING(cTranNo, 1,3) = 'CM-' ")
    
    ExtractRS "SELECT A.cCompanyID, A.cTranNo, A.lPrintPosted AS lUpload, A.dDate, A.cCode, B.cName, A.nGross, A.cRemarks, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, A.cApprovedBy " & _
        "FROM CM A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCompanyID = B.cCompanyID AND A.cCode = B.cCode " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.dDate ", sFilePath & "CM.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM CM_T A " & _
        "LEFT OUTER JOIN CM B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "CM_T.itg "
        
    ExtractRS "SELECT A.cCompanyID, A.cTranNo, A.lPrintPosted AS lUpload, A.dDate, A.cType, A.cCode, B.cName, A.nGross, A.cRemarks, " & _
        "A.lSalesReturn, A.cWRRNo, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, A.cApprovedBy " & _
        "FROM AR A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' AND A.lApproved = 1 AND SUBSTRING(A.cTranNo, 1,3) = 'CM-' " & _
        "ORDER BY A.dDate ", sFilePath & "CM_AR.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM AR_T A " & _
        "LEFT OUTER JOIN AR B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' AND B.lApproved = 1 AND SUBSTRING(A.cTranNo, 1,3) = 'CM-' " & _
        "ORDER BY A.cTranNo ", sFilePath & "CM_AR_T.itg "
        
    'Audit trail
    UpdateLogFile "Downloading Transaction", "CM Product", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
End Function

Public Function DownloadCollection(tdteFrom As Date, tdteTo As Date) ' As Boolean

    cn.BeginTrans
    cn.Execute ("UPDATE PR SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cCode IS NOT NULL AND RTRIM(LTRIM(cCode)) <> '') AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT A.cCompanyID, A.cTranNo, A.lPrintPosted AS lUpload, A.cCode, B.cName, A.dDate, A.nPDC, A.nCash, A.nEWT, A.nInvTotal, A.nPayTotal, A.nOthersTotal, " & _
        "A.cRemarks, A.lPosted, A.lClosed, A.lCancelled, A.cApprovedBy, A.nEVAT, A.cCollectionNo, A.dCollectionPrint " & _
        "FROM PR A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCompanyID = B.cCompanyID AND A.cCode = B.cCode " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.dDate ", sFilePath & "PR.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM PR_T A " & _
        "LEFT OUTER JOIN PR B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "PR_T.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM PR_C A " & _
        "LEFT OUTER JOIN PR B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "PR_C.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM PR_O A " & _
        "LEFT OUTER JOIN PR B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "PR_O.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM PR_MAP A " & _
        "LEFT OUTER JOIN PR B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "PR_MAP.itg "
    
    'Audit trail
    UpdateLogFile "Downloading Transaction", "Collection", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
End Function

Public Function DownloadWRR(tdteFrom As Date, tdteTo As Date) As Boolean
    
    cn.BeginTrans
    cn.Execute ("UPDATE WRR SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cCode IS NOT NULL AND RTRIM(LTRIM(cCode)) <> '') AND cCompanyID = '" & COID & "'")
            
    ExtractRS "SELECT A.cCompanyID, A.cWRRNo, A.lPrintPosted AS lUpload, A.dDate, A.cType, A.cCode, B.cName, A.cRemarks, A.nGross, A.nReturn, A.nPaid, " & _
        "A.nCredit, A.nDebit, A.nBalance, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, A.cApprovedBy, A.cPCCode, A.cPullOutNo " & _
        "FROM WRR A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCompanyID = B.cCompanyID AND A.cCode = B.cCode " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.dDate ", sFilePath & "WRR.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM WRR_T A " & _
        "LEFT OUTER JOIN WRR B ON A.cWRRNo = B.cWRRNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cWRRNo ", sFilePath & "WRR_T.itg "
        
    'Audit trail
    UpdateLogFile "Downloading Transaction", "WRR", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
End Function

Public Function DownloadPO(tdteFrom As Date, tdteTo As Date) As Boolean
        
    cn.BeginTrans
    cn.Execute ("UPDATE PRODUCTION SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND cCompanyID = '" & COID & "'")
        
    ExtractRS "SELECT cCompanyID, cTranNo, lPrintPosted AS lUpload, dDate, dNeeded, cType, cRefNo, " & _
        "cRemarks, cDRNo, lRRReady, lPosted, lClosed, lCancelled, lApproved, cApprovedBy, lServed " & _
        "FROM PRODUCTION " & _
        "WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND cCompanyID = '" & COID & "' " & _
        "ORDER BY dDate ", sFilePath & "PRODUCTION.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM PRODUCTION_BM A " & _
        "LEFT OUTER JOIN PRODUCTION B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "PRODUCTION_BM.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM PRODUCTION_FG A " & _
        "LEFT OUTER JOIN PRODUCTION B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "PRODUCTION_FG.itg "
        
    ExtractRS "SELECT A.cCompanyID, A.cTranNo, A.lPrintPosted AS lUpload, A.dDate, A.cType, A.cCode, B.cName, A.nGross, A.cRemarks, " & _
        "A.lSalesReturn, A.cWRRNo, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, A.cApprovedBy " & _
        "FROM AR A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' AND A.lApproved = 1 AND SUBSTRING(A.cTranNo, 1,3) = 'CM-' " & _
        "ORDER BY A.dDate ", sFilePath & "CM_AR.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM AR_T A " & _
        "LEFT OUTER JOIN AR B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' AND B.lApproved = 1 AND SUBSTRING(A.cTranNo, 1,3) = 'CM-' " & _
        "ORDER BY A.cTranNo ", sFilePath & "CM_AR_T.itg "
        
    'Audit trail
    UpdateLogFile "Downloading Transaction", "PO", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
End Function

Public Function DownloadInvAdj(tdteFrom As Date, tdteTo As Date) As Boolean
        
    cn.BeginTrans
    cn.Execute ("UPDATE INVADJ SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT cCompanyID, cTranNo, lPrintPosted AS lUpload, dDate, cCategory, dCountDate, cPDFrom, cPDTo, " & _
        "lCancelled, lPosted, lClosed " & _
        "FROM INVADJ " & _
        "WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND cCompanyID = '" & COID & "' " & _
        "ORDER BY dDate ", sFilePath & "INVADJ.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM INVADJ_T A " & _
        "LEFT OUTER JOIN INVADJ B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "INVADJ_T.itg "
                
    'Audit trail
    UpdateLogFile "Downloading Transaction", "Inv Adjustment", "Downloaded"
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
End Function

Public Function DownloadInvTransfer(tdteFrom As Date, tdteTo As Date) As Boolean
        
    cn.BeginTrans
    cn.Execute ("UPDATE TRANSFER SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT cCompanyID, cTranNo, lPrintPosted AS lUpload, dDate, cRemarks, lServed, lPosted, lClosed, lCancelled, lApproved, cApprovedBy " & _
        "FROM TRANSFER " & _
        "WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND cCompanyID = '" & COID & "' " & _
        "ORDER BY dDate ", sFilePath & "TRANSFER.itg "
        
    ExtractRS "SELECT A.* " & _
        "FROM TRANSFER_IN A " & _
        "LEFT OUTER JOIN TRANSFER B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "TRANSFER_IN.itg "
                
    ExtractRS "SELECT A.* " & _
        "FROM TRANSFER_OUT A " & _
        "LEFT OUTER JOIN TRANSFER B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.cTranNo ", sFilePath & "TRANSFER_OUT.itg "
        
    'Audit trail
    UpdateLogFile "Downloading Transaction", "Inv Transfer", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
End Function

Public Function DownloadEWT(tdteFrom As Date, tdteTo As Date) As Boolean
        
    cn.BeginTrans
    cn.Execute ("UPDATE EWT SET lUpload = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT A.cCompanyID, A.nEWTNo, A.lUpload, A.dDate, A.cORNo, A.dORDate, A.cCode, B.cName, A.cInvNo, A.dInvDate, A.cARNo, A.nEWTAmt, A.lPosted " & _
        "FROM EWT A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCompanyID = B.cCompanyID AND A.cCode = B.cCode " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND A.cCompanyID = '" & COID & "' " & _
        "ORDER BY A.dDate ", sFilePath & "EWT.itg "
    
    'Audit trail
    UpdateLogFile "Downloading Transaction", "EWT", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
        
End Function

Public Function DownloadEVAT(tdteFrom As Date, tdteTo As Date) As Boolean
        
    cn.BeginTrans
    cn.Execute ("UPDATE EVAT SET lUpload = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT A.cCompanyID, A.nEVATNo, A.lUpload, A.dDate, A.cORNo, A.dORDate, A.cCode, B.cName, A.cInvNo, A.dInvDate, A.cARNo, A.nEVATAmt, A.lPosted " & _
        "FROM EVAT A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCompanyID = B.cCompanyID AND A.cCode = B.cCode " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND A.cCompanyID = '" & COID & "' ORDER BY A.dDate ", sFilePath & "EVAT.itg "
    
    'Audit trail
    UpdateLogFile "Downloading Transaction", "EVAT", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else
        cn.RollbackTrans
    End If
        
End Function

Public Function DownloadCMEWTEVAT(tdteFrom As Date, tdteTo As Date) As Boolean
    cn.BeginTrans
    cn.Execute ("UPDATE CM_EWTEVAT SET lPrintPosted = 1 WHERE dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' AND (cCode IS NOT NULL AND RTRIM(LTRIM(cCode)) <> '') AND cCompanyID = '" & COID & "'")
    
    ExtractRS "SELECT  A.cCompanyID, A.cTranNo, A.lPrintPosted AS lUpload, A.dDate, A.cCode, A.nGross, A.cRemarks, A.lPosted, A.lClosed, A.lCancelled, A.lApproved, A.cApprovedBy, A.lPrintPosted " & _
        "FROM CM_EWTEVAT A " & _
        "LEFT OUTER JOIN CLIENT B ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
        "WHERE A.dDate BETWEEN '" & CStr(tdteFrom) & "' AND '" & CStr(tdteTo) & "' " & _
        "AND (A.cCode IS NOT NULL AND RTRIM(LTRIM(A.cCode)) <> '') AND A.cCompanyID = '" & COID & "' " & _
        "AND A.lApproved = 1 ORDER BY A.dDate ", sFilePath & "CM_EWTEVAT.itg "
        
    ExtractRS "SELECT A.cCompanyID, A.nIdentity, A.cTranNo, A.cInvNo, A.nRefNo, A.cType, A.cAcctNo, A.cTitle, A.cCreditDesc, A.nAmount " & _
        "FROM CM_EWTEVAT_T A " & _
        "LEFT OUTER JOIN CM_EWTEVAT B ON A.cTranNo = B.cTranNo AND A.cCompanyID = B.cCompanyID " & _
        "WHERE B.dDate BETWEEN '" & CStr(tdteFrom) & "' AND " & _
        "'" & CStr(tdteTo) & "' AND (B.cCode IS NOT NULL AND " & _
        "RTRIM(LTRIM(B.cCode)) <> '') AND A.cCompanyID = '" & COID & "' AND B.lApproved = 1 " & _
        "ORDER BY A.cTranNo ", sFilePath & "CM_EWTEVAT_T.itg "
        
    'Audit trail
    UpdateLogFile "Downloading Transaction", "CM EWTEVAT Adjustment", "Downloaded"
    
    If lSpaceOK Then
        cn.CommitTrans
    Else: cn.RollbackTrans
    End If
End Function

Public Function FileExists(FileName As String) As Boolean
'The FileExists() function returns a value
'of TRUE if the specified file exists, or
'FALSE if it doesn't.
Dim Msg As String
' Turn on error trapping so error handler responds
' if any error is detected.
On Error GoTo CheckError
    FileExists = (Dir(FileName) <> "")
    ' Avoid executing error handler if no error occurs.
    Exit Function

CheckError:                 ' Branch here if error occurs.
' Define constants to represent intrinsic Visual Basic error
' codes.
Const mnErrDiskNotReady = 71, mnErrDeviceUnavailable = 68
' vbExclamation, vbOK, vbCancel, vbCritical, and vbOKCancel are
'constants defined in the VBA type library.
If (err.Number = mnErrDiskNotReady) Then
    Msg = "Put a floppy disk in the drive and close the door."
    ' Display message box with an exclamation mark icon and with
    ' OK and Cancel buttons.
    If MsgBox(Msg, vbExclamation & vbOKCancel) = vbOK Then
        Resume
    Else
        Resume Next
    End If
ElseIf err.Number = mnErrDeviceUnavailable Then
    Msg = "This drive or path does not exist: " & FileName
    MsgBox Msg, vbExclamation
    Resume Next
Else
    Msg = "Unexpected error #" & str(err.Number) & " occurred: " _
    & err.Description
    ' Display message box with Stop sign icon and OK button.
    MsgBox Msg, vbCritical
    Stop
End If
Resume
End Function

Private Sub ExtractRS(sCommandString As String, sFilePath As String)
On Error GoTo TheSource
Dim lcCmdString As String
Dim rsSet As ADODB.Recordset

    lSpaceOK = True
    
    strTranNo = ""
    lcCmdString = sCommandString
        
    Set rsSet = oSQLConn.Execute(lcCmdString)
    If Not rsSet Is Nothing Then
        If (FileExists(sFilePath)) Then
            Kill sFilePath
        End If
        rsSet.Save sFilePath, adPersistADTG
    End If
    
    Set rsSet = Nothing

TheSource:
    'There is not enough space on the disk.
    If err.Number = -2147024784 Then
        MsgBox "There is not enough space on the disk.", vbExclamation, msgTtl
        lSpaceOK = False
    End If

End Sub
